package com.landinfo.service.impl;

import com.landinfo.domain.BookMark;
import com.landinfo.repository.BookMarkRepository;
import com.landinfo.service.BookMarkService;
import com.landinfo.service.dto.BookMarkDTO;
import com.landinfo.service.mapper.BookMarkMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing BookMark.
 */
@Service
@Transactional
public class BookMarkServiceImpl implements BookMarkService {

  private final Logger log = LoggerFactory.getLogger(BookMarkServiceImpl.class);

  private final BookMarkRepository bookMarkRepository;

  private final BookMarkMapper bookMarkMapper;

  public BookMarkServiceImpl(BookMarkRepository bookMarkRepository, BookMarkMapper bookMarkMapper) {
    this.bookMarkRepository = bookMarkRepository;
    this.bookMarkMapper = bookMarkMapper;
  }

  /**
   * Save a bookMark.
   *
   * @param bookMarkDTO the entity to save
   * @return the persisted entity
   */
  @Override
  public BookMarkDTO save(BookMarkDTO bookMarkDTO) {
    log.debug("Request to save BookMark : {}", bookMarkDTO);
    BookMark bookMark = bookMarkMapper.bookMarkDTOToBookMark(bookMarkDTO);
    bookMark = bookMarkRepository.save(bookMark);
    BookMarkDTO result = bookMarkMapper.bookMarkToBookMarkDTO(bookMark);
    return result;
  }

  /**
   * Get all the bookMarks.
   *
   * @return the list of entities
   */
  @Override
  @Transactional(readOnly = true)
  public List<BookMarkDTO> findAll() {
    log.debug("Request to get all BookMarks");
    List<BookMarkDTO> result = bookMarkRepository.findAll().stream()
      .map(bookMarkMapper::bookMarkToBookMarkDTO)
      .collect(Collectors.toCollection(LinkedList::new));

    return result;
  }

  @Override
  public List<BookMarkDTO> getAllBookMarksByUser(String loginName) {
    try{
      return bookMarkRepository.findByUserIsCurrentUser(loginName).stream()
              .map(bookMarkMapper::bookMarkToBookMarkDTO)
              .collect(Collectors.toCollection(LinkedList::new));
    }catch (Exception ex){
      return null;
    }
  }

  /**
   * Get one bookMark by id.
   *
   * @param id the id of the entity
   * @return the entity
   */
  @Override
  @Transactional(readOnly = true)
  public BookMarkDTO findOne(String id) {
    log.debug("Request to get BookMark : {}", id);
    BookMark bookMark = bookMarkRepository.findById(id).get();
    BookMarkDTO bookMarkDTO = bookMarkMapper.bookMarkToBookMarkDTO(bookMark);
    return bookMarkDTO;
  }

  /**
   * Delete the  bookMark by id.
   *
   * @param id the id of the entity
   */
  @Override
  public void delete(String id) {
    log.debug("Request to delete BookMark : {}", id);
    bookMarkRepository.deleteById(id);
  }
}
